using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Collections;

namespace sCicero
{
    public struct WorkArea
    {
        public DataRow oWorkArea;
        public DataTable oFilesTable;
    }

    public class WorkAreas : Base
    {
        private const string WORKAREAS = "workareas";
        private const string FILE_WORKAREAS = "fileslink_workareas";
        private WorkArea m_oWorkArea;

        public WorkAreas(ref Data oData)
        {
            m_Table = WORKAREAS;
            m_oData = oData;
        }
        public override DataRow Get(object oKey)
        {
            return m_oData.GetGenericDataTable("workareas").Select("id = " + oKey.ToString())[0];
        }
        
        public Errorcode New(string sName)
        {
            Hashtable oCols = new Hashtable();
            oCols.Add("name", sName);

            Errorcode oE = m_oData.New(ref m_oData, WORKAREAS, oCols);
            if (oE != Errorcode.NOERROR)
                m_LastError = m_oData.LastError();
            return oE;
        }

        public WorkArea GetObject(int nWorkAreaID)
        {
            try
            {
                m_oWorkArea.oWorkArea = Get(nWorkAreaID);
                string sWhere = "";
                foreach (DataRow oDr in m_oData.GetGenericDataTable(FILE_WORKAREAS).Select("workarea_id = " + nWorkAreaID.ToString()))
                {
                    sWhere += oDr["file_id"].ToString() + ",";
                }
                m_oWorkArea.oFilesTable = null;
                if (sWhere.Length > 0)
                {
                    sWhere = sWhere.Substring(0, sWhere.Length - 1);
                    m_oWorkArea.oFilesTable = m_oData.GetDataTablefromRows(m_oData.GetGenericDataTable("files").Select("id in (" + sWhere + ")"));
                }
            }
            catch (Exception ex)
            {
                //nothing
            }
            return m_oWorkArea;
        }
    }
}
